<?php
namespace app\common\wx_payment;
use think\cache\driver\Redis;
use think\Exception;

/**
 * 微信支付(企业付款到零钱)
 * Class WxPayment
 */
class Payment
{
    public function unicode() {
        $str = uniqid(mt_rand(),1);
        $str=sha1($str);
        return md5($str);
    }

    public function arraytoxml($data) {
        $str='<xml>';
        foreach($data as $k=>$v) {
            $str.='<'.$k.'>'.$v.'</'.$k.'>';
        }
        $str.='</xml>';
        return $str;
    }

    public function xmltoarray($xml) {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring),true);
        return $val;
    }

    public function curl($param="",$url) {
        $cert_root = APP_PATH . 'cert_wx/';
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();                   //初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);         //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);          //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);           //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);      // 增加 HTTP Header（头）里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch,CURLOPT_SSLCERT,$cert_root . 'apiclient_cert.pem'); //这个是证书的位置
        curl_setopt($ch,CURLOPT_SSLKEY,$cert_root . 'apiclient_key.pem'); //这个也是证书的位置
        $data = curl_exec($ch);                 //运行curl
        curl_close($ch);
        return $data;
    }

    /**
     * 企业支付到零钱
     * @param string $openid 用户openid
     * @param number $amount 提现金额
     * @param $trade_no
     * @param $desc
     * @return array
     */
    public function sendPacket_f($openid, $amount, $trade_no, $desc) {
        try{
            $data['mch_appid'] = config('wx_payment.mch_appid');//商户的应用appid
            $data['mchid'] = config('wx_payment.mchid');//商户ID
            $data['nonce_str'] = $this->unicode();//unicode();//这个据说是唯一的字符串下面有方法
            $data['partner_trade_no'] = $trade_no;//.time();//这个是订单号。
            $data['openid'] = $openid;//这个是授权用户的openid。。这个必须得是用户授权才能用
            $data['check_name'] = 'NO_CHECK';//这个是设置是否检测用户真实姓名的
            //$data['re_user_name']='';//用户的真实名字
            $data['amount'] = 100*$amount;//提现金额
            $data['desc'] = $desc;//订单描述
            $data['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];//这个最烦了，，还得获取服务器的ip
            $secrect_key = config('wx_payment.str');//这个就是个API密码。32位的。。随便MD5一下就可以了
            $data = array_filter($data);
            ksort($data);
            $str = '';
            foreach ($data as $k => $v) {
                $str .= $k . '=' . $v . '&';
            }
            $str .= 'key=' . $secrect_key;
            $data['sign'] = md5($str);
            //print_r($data);
            $xml = $this->arraytoxml($data);
            $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
            $res = $this->curl($xml, $url);
            $result = $this->xmltoarray($res);
            if ($result['result_code'] == 'FAIL') {
                throw new Exception($result['err_code_des']);
            }
            return ['code' => 1, 'data' => $result];
        } catch (\Exception $e) {
            return ['code' => 0, 'error' => $e->getMessage()];
        }
    }

    //    public function shizhi() {
//        $redis = new Redis();
//        $token_redis_key = "WX_CONFIG:OriginalId:gh_010884bf5e53";
//        $wx_config = $redis->get($token_redis_key);
//        $wx_config['appid'];
//        print_r($wx_config);
//        echo '</br>ok';
//    }

}